package com.baide.sanguanheyi.swing;

import com.baide.sanguanheyi.ct.HeartBeatClient;
import com.baide.sanguanheyi.plc.NioServer;
import com.baide.sanguanheyi.weight.WeightAccept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName TableFrame
 * @Description TODO
 * @Author wangbing
 * @Date 2020/5/19 14:03
 **/
@Component
public class TableFrame extends JFrame {

    @Autowired
    SelContent selContent;
    @Autowired
    MainContent mainContent;
    @Autowired
    HeartBeatClient ctClient;//ct 线程
    @Autowired
    NioServer plcServer;//plc和相机
    @Autowired
    WeightAccept weightAccept;//称重机
    static JFrame jFrame;
    public TableFrame(SelContent selContent, MainContent mainContent, HeartBeatClient ctClient, NioServer plcServer, WeightAccept weightAccept) {
        setTitle("百德海关查验三关合一系统");
        jFrame=this;
        dispose();
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        add(tablePanel(selContent, mainContent), BorderLayout.CENTER);
        setVisible(true);
        ExecutorService pool = Executors.newFixedThreadPool(3);
        ctClient.setCtCallBack(mainContent);//设置回调
        mainContent.setCtSendMsg(ctClient);//发送信息方法
        weightAccept.setCallBack(mainContent);
        plcServer.setCallBack(mainContent);
        mainContent.setPlcSendMsg(plcServer);
        pool.execute(ctClient);//启动并监听
        pool.execute(weightAccept);
        pool.execute(plcServer);
        pool.shutdown();
    }

    public static JFrame getjFrame() {
        return jFrame;
    }

    private JPanel tablePanel(SelContent selContent, MainContent mainContent) {
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(1, 1));
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("过包", mainContent);
        tabbedPane.addTab("查询及导出", selContent);
        jPanel.add(tabbedPane);
        return jPanel;
    }
}
